package com.server.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.server.db.imp.JzdlImp;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.server.db.imp.YhImp;
import com.sandking.io.SK_ByteBuff;
import com.sandking.tools.SK_Plus;
import java.util.List;
import com.server.db.imp.YhjzImp;
import com.server.db.imp.BydlImp;
import java.util.HashMap;

/**
 * 用户建筑
 */
public class Yhjz {

	public static final String TABLE_NAME = "用户建筑";
	public static final String CLASS_NAME = "Yhjz"; 
	static final int MAX_GET = Short.MAX_VALUE;
	
	//索引名称
	public static final String  Id_IndexName = "id";
	public static final String  Yh_id_IndexName = "yh_id";
	
	//对象版本号	
	private int _v = 0;
	public int _nextVer(){
		if(_v > MAX_GET) _v = 0;
		return ++_v;
	}
	
	public int _getVer(){
		return _v;
	}
	
	//是否是源对象
	private boolean _source = false;
	
	public boolean is_source() {
		return _source;
	}

	public void set_source(boolean _source) {
		this._source = _source;
	}
	
	//Cache 中的索引
	private String  id_Unique = null;
	private String  yh_id_Index = null;
	public Yhjz updatePrimaryIndex(){
		return updatePrimaryIndex(this);
	}
	
	public static List<Yhjz> updatePrimaryIndex(List<Yhjz> yhjzs){
		for(Yhjz yhjz : yhjzs){
			updatePrimaryIndex(yhjz);	
		}
		return yhjzs;
	}
	
	public static Yhjz updatePrimaryIndex(Yhjz yhjz){
		String field = SK_Plus.b(yhjz.getId()).e();
		yhjz.setId_Unique(field);
		
		field = SK_Plus.b(yhjz.getYh_id()).e();
		yhjz.setYh_id_Index(field);
		
		return yhjz;
	}
	
	
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/* 可能需要补救的字段 */
	private SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	
	public SK_ByteBuff getSK_ByteBuff(){
		return this.byteBuff;
	}
	
	/** id */
	private int id;
	
	/** 建筑id */
	private int jzid;
	
	/** 建筑lvl */
	private int jzlvl;
	
	/** x */
	private int x;
	
	/** y */
	private int y;
	
	/** 用户_id */
	private int yh_id;
	
	
	public Yhjz() {
		super();
		this._source = true;
	}
	
	public Yhjz(int id, int jzid, int jzlvl, int x, int y, int yh_id) {
		super();
		this.id = id;
		this.jzid = jzid;
		this.jzlvl = jzlvl;
		this.x = x;
		this.y = y;
		this.yh_id = yh_id;
		this._source = true;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public void setUpdateColumns(Map<String, Object> updateColumns) {
		this.updateColumns = updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		changeSetId(id);
		if(!this._source){
			byteBuff.writeInt(1);
			byteBuff.writeInt(id);
		}
	}
	
	private void changeSetId(int id) {
		this.id = id;
		if(!this._source){
			addUpdateColumn("id",id);
		}
	}
	
	public void changeIdWith(int id){
		this.id += id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(2);
			byteBuff.writeInt(id);
		}
	}
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(3);
			byteBuff.writeInt(id);
			byteBuff.writeInt(min);
		}	
	}
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(4);
			byteBuff.writeInt(id);
			byteBuff.writeInt(max);
		}
	}
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(5);
			byteBuff.writeInt(id);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getJzid() {
		return jzid;
	}
	
	public void setJzid(int jzid) {
		changeSetJzid(jzid);
		if(!this._source){
			byteBuff.writeInt(6);
			byteBuff.writeInt(jzid);
		}
	}
	
	private void changeSetJzid(int jzid) {
		this.jzid = jzid;
		if(!this._source){
			addUpdateColumn("建筑id",jzid);
		}
	}
	
	public void changeJzidWith(int jzid){
		this.jzid += jzid;
		changeSetJzid(this.jzid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(7);
			byteBuff.writeInt(jzid);
		}
	}
	public void changeJzidWithMin(int jzid,int min){
		this.jzid += jzid;
		this.jzid = this.jzid < min ? min : this.jzid;
		changeSetJzid(this.jzid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(8);
			byteBuff.writeInt(jzid);
			byteBuff.writeInt(min);
		}	
	}
	public void changeJzidWithMax(int jzid,int max){
		this.jzid += jzid;
		this.jzid = this.jzid > max ? max : this.jzid;
		changeSetJzid(this.jzid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(9);
			byteBuff.writeInt(jzid);
			byteBuff.writeInt(max);
		}
	}
	public void changeJzidWithMaxMin(int jzid,int max,int min){
		this.jzid += jzid;
		this.jzid = this.jzid < min ? min : this.jzid;
		this.jzid = this.jzid > max ? max : this.jzid;
		changeSetJzid(this.jzid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(10);
			byteBuff.writeInt(jzid);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getJzlvl() {
		return jzlvl;
	}
	
	public void setJzlvl(int jzlvl) {
		changeSetJzlvl(jzlvl);
		if(!this._source){
			byteBuff.writeInt(11);
			byteBuff.writeInt(jzlvl);
		}
	}
	
	private void changeSetJzlvl(int jzlvl) {
		this.jzlvl = jzlvl;
		if(!this._source){
			addUpdateColumn("建筑lvl",jzlvl);
		}
	}
	
	public void changeJzlvlWith(int jzlvl){
		this.jzlvl += jzlvl;
		changeSetJzlvl(this.jzlvl);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(12);
			byteBuff.writeInt(jzlvl);
		}
	}
	public void changeJzlvlWithMin(int jzlvl,int min){
		this.jzlvl += jzlvl;
		this.jzlvl = this.jzlvl < min ? min : this.jzlvl;
		changeSetJzlvl(this.jzlvl);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(13);
			byteBuff.writeInt(jzlvl);
			byteBuff.writeInt(min);
		}	
	}
	public void changeJzlvlWithMax(int jzlvl,int max){
		this.jzlvl += jzlvl;
		this.jzlvl = this.jzlvl > max ? max : this.jzlvl;
		changeSetJzlvl(this.jzlvl);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(14);
			byteBuff.writeInt(jzlvl);
			byteBuff.writeInt(max);
		}
	}
	public void changeJzlvlWithMaxMin(int jzlvl,int max,int min){
		this.jzlvl += jzlvl;
		this.jzlvl = this.jzlvl < min ? min : this.jzlvl;
		this.jzlvl = this.jzlvl > max ? max : this.jzlvl;
		changeSetJzlvl(this.jzlvl);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(15);
			byteBuff.writeInt(jzlvl);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getX() {
		return x;
	}
	
	public void setX(int x) {
		changeSetX(x);
		if(!this._source){
			byteBuff.writeInt(16);
			byteBuff.writeInt(x);
		}
	}
	
	private void changeSetX(int x) {
		this.x = x;
		if(!this._source){
			addUpdateColumn("x",x);
		}
	}
	
	public void changeXWith(int x){
		this.x += x;
		changeSetX(this.x);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(17);
			byteBuff.writeInt(x);
		}
	}
	public void changeXWithMin(int x,int min){
		this.x += x;
		this.x = this.x < min ? min : this.x;
		changeSetX(this.x);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(18);
			byteBuff.writeInt(x);
			byteBuff.writeInt(min);
		}	
	}
	public void changeXWithMax(int x,int max){
		this.x += x;
		this.x = this.x > max ? max : this.x;
		changeSetX(this.x);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(19);
			byteBuff.writeInt(x);
			byteBuff.writeInt(max);
		}
	}
	public void changeXWithMaxMin(int x,int max,int min){
		this.x += x;
		this.x = this.x < min ? min : this.x;
		this.x = this.x > max ? max : this.x;
		changeSetX(this.x);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(20);
			byteBuff.writeInt(x);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getY() {
		return y;
	}
	
	public void setY(int y) {
		changeSetY(y);
		if(!this._source){
			byteBuff.writeInt(21);
			byteBuff.writeInt(y);
		}
	}
	
	private void changeSetY(int y) {
		this.y = y;
		if(!this._source){
			addUpdateColumn("y",y);
		}
	}
	
	public void changeYWith(int y){
		this.y += y;
		changeSetY(this.y);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(22);
			byteBuff.writeInt(y);
		}
	}
	public void changeYWithMin(int y,int min){
		this.y += y;
		this.y = this.y < min ? min : this.y;
		changeSetY(this.y);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(23);
			byteBuff.writeInt(y);
			byteBuff.writeInt(min);
		}	
	}
	public void changeYWithMax(int y,int max){
		this.y += y;
		this.y = this.y > max ? max : this.y;
		changeSetY(this.y);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(24);
			byteBuff.writeInt(y);
			byteBuff.writeInt(max);
		}
	}
	public void changeYWithMaxMin(int y,int max,int min){
		this.y += y;
		this.y = this.y < min ? min : this.y;
		this.y = this.y > max ? max : this.y;
		changeSetY(this.y);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(25);
			byteBuff.writeInt(y);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getYh_id() {
		return yh_id;
	}
	
	public void setYh_id(int yh_id) {
		changeSetYh_id(yh_id);
		if(!this._source){
			byteBuff.writeInt(26);
			byteBuff.writeInt(yh_id);
		}
	}
	
	private void changeSetYh_id(int yh_id) {
		this.yh_id = yh_id;
		if(!this._source){
			addUpdateColumn("用户_id",yh_id);
		}
	}
	
	public void changeYh_idWith(int yh_id){
		this.yh_id += yh_id;
		changeSetYh_id(this.yh_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(27);
			byteBuff.writeInt(yh_id);
		}
	}
	public void changeYh_idWithMin(int yh_id,int min){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id < min ? min : this.yh_id;
		changeSetYh_id(this.yh_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(28);
			byteBuff.writeInt(yh_id);
			byteBuff.writeInt(min);
		}	
	}
	public void changeYh_idWithMax(int yh_id,int max){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id > max ? max : this.yh_id;
		changeSetYh_id(this.yh_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(29);
			byteBuff.writeInt(yh_id);
			byteBuff.writeInt(max);
		}
	}
	public void changeYh_idWithMaxMin(int yh_id,int max,int min){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id < min ? min : this.yh_id;
		this.yh_id = this.yh_id > max ? max : this.yh_id;
		changeSetYh_id(this.yh_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(30);
			byteBuff.writeInt(yh_id);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	
	//id
	public List<Bydl> getBydlsFkYhjz_id(){
		return BydlImp.getByYhjz_id(id);
	}
	
	//id
	public List<Jzdl> getJzdlsFkYhjz_id(){
		return JzdlImp.getByYhjz_id(id);
	}
	
	//用户_id
	public Yh getYhPkYh_id(){
		return YhImp.getById(yh_id);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	/**
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	*/
	public Yhjz clone(){
		Yhjz yhjz = new Yhjz();
		yhjz.setId(this.getId());
		yhjz.setJzid(this.getJzid());
		yhjz.setJzlvl(this.getJzlvl());
		yhjz.setX(this.getX());
		yhjz.setY(this.getY());
		yhjz.setYh_id(this.getYh_id());
		yhjz._v = _v;
		yhjz._source = false;
		return yhjz.updatePrimaryIndex(); 
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("jzid", this.jzid);
        result.put("jzlvl", this.jzlvl);
        result.put("x", this.x);
        result.put("y", this.y);
        result.put("yh_id", this.yh_id);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("建筑id", this.jzid);
        result.put("建筑lvl", this.jzlvl);
        result.put("x", this.x);
        result.put("y", this.y);
        result.put("用户_id", this.yh_id);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	    byteBuff.writeInt(this.id);
	    byteBuff.writeInt(this.jzid);
	    byteBuff.writeInt(this.jzlvl);
	    byteBuff.writeInt(this.x);
	    byteBuff.writeInt(this.y);
	    byteBuff.writeInt(this.yh_id);
	    return byteBuff.toByte();
    }
    
    public byte[] toSKBytes(){
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
    	byteBuff.writeMap(toMap());
	    return byteBuff.toByte();
    }
    
     public static Yhjz createForBytes(byte[] _byte) throws Exception {
     	SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     	Yhjz yhjz = new Yhjz();
	    yhjz.id = byteBuff.readInt(null);
	    yhjz.jzid = byteBuff.readInt(null);
	    yhjz.jzlvl = byteBuff.readInt(null);
	    yhjz.x = byteBuff.readInt(null);
	    yhjz.y = byteBuff.readInt(null);
	    yhjz.yh_id = byteBuff.readInt(null);
	    return yhjz.updatePrimaryIndex();
     }
     
     public static Yhjz createForSKBytes(byte[] _byte){
     		SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     		@SuppressWarnings("unchecked")
     		Map<String,Object> map = byteBuff.readMap(null);
	     	Yhjz yhjz = Yhjz.createForMap(map);
		    return yhjz.updatePrimaryIndex();
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhjz> createForColumnNameList(List<Map<String, Object>> list){
    	List<Yhjz> yhjzs = new ArrayList<Yhjz>();
		for (Map<String, Object> map : list) {
			yhjzs.add(createForColumnNameMap(map));
		}
		return yhjzs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhjz createForColumnNameMap(Map<String, Object> map){
    	Yhjz obj = new Yhjz();
	    obj.id = SK_Map.getInt("id", map);
	    obj.jzid = SK_Map.getInt("建筑id", map);
	    obj.jzlvl = SK_Map.getInt("建筑lvl", map);
	    obj.x = SK_Map.getInt("x", map);
	    obj.y = SK_Map.getInt("y", map);
	    obj.yh_id = SK_Map.getInt("用户_id", map);
        return obj.updatePrimaryIndex();
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhjz> createForList(List<Map<String, Object>> list){
    	List<Yhjz> yhjzs = new ArrayList<Yhjz>();
		for (Map<String, Object> map : list) {
			yhjzs.add(createForColumnNameMap(map));
		}
		return yhjzs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhjz createForMap(Map<String, Object> map){
    	Yhjz obj = new Yhjz();
	    obj.id = SK_Map.getInt("id", map);
	    obj.jzid = SK_Map.getInt("jzid", map);
	    obj.jzlvl = SK_Map.getInt("jzlvl", map);
	    obj.x = SK_Map.getInt("x", map);
	    obj.y = SK_Map.getInt("y", map);
	    obj.yh_id = SK_Map.getInt("yh_id", map);
        return obj.updatePrimaryIndex();
    }
    
    public static Yhjz createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Yhjz> createForJson(List<String> jsons){
    	List<Yhjz> yhjzs = new ArrayList<Yhjz>();
    	for(String json : jsons){
    		yhjzs.add(createForJson(json));
    	}
    	return yhjzs;
    }
    
    /** 级联删除(延迟入库) */
    public boolean deleteAndSon(){
    	return false;
    }
    
    /** 级联删除(及时入库) */
    public boolean deleteAndSonAndFlush(){
    	return false;
    }
    
    /** 延迟插入数据库 */
    public Yhjz insert(){
    	return YhjzImp.insert(this);
    }
    /** 延迟更新数据库 */
    public Yhjz update(){
    	return YhjzImp.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return YhjzImp.delete(this);
    }
    /** 即时插入数据库 */
    public Yhjz insertAndFlush(){
    	return YhjzImp.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Yhjz updateAndFlush(){
    	return YhjzImp.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return YhjzImp.deleteAndFlush(this);
    }
    
    
    
    //--------------------------------Cache 中的索引 GET & SET
	public String getId_Unique(){
		return id_Unique;
	}
	
	public void setId_Unique(String id_Unique){
		this.id_Unique = id_Unique;
	}
	
	public String getYh_id_Index(){
		return yh_id_Index;
	}
	
	public void setYh_id_Index(String yh_id_Index){
		this.yh_id_Index = yh_id_Index;
	}
	
	
	
	/** 取得改变的索引 */
	public Map<String,String> getUpdateIndexs(){
		Map<String,String> indexNames = new HashMap<String,String>();
		if(!this.getId_Unique().equals(SK_Plus.b(id).e())){
			indexNames.put(Yhjz.Id_IndexName,SK_Plus.b(id).e());
			indexNames.put("isUpdtaeUnique","true");
		}
			
		if(!this.getYh_id_Index().equals(SK_Plus.b(yh_id).e()))
			indexNames.put(Yhjz.Yh_id_IndexName,SK_Plus.b(yh_id).e());
			
		return indexNames;
	}
}